package com.doxiaopu.students.organization.service.impl;

import com.doxiaopu.students.organization.entity.Organization;
import com.doxiaopu.students.organization.mapper.OrganizationMapper;
import com.doxiaopu.students.organization.service.IOrganizationService;
import com.doxiaopu.students.organization.vo.OrganizationVO;
import com.doxiaopu.students.user.entity.User;
import com.doxiaopu.students.user.mapper.UserMapper;
import com.doxiaopu.students.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Primary
public class OrganizationServiceImpl implements IOrganizationService {
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IUserService iUserService;

    @Override
    @Transactional
    public Integer createCouncil(Organization organization,Integer id) {
//        创建用户信息
        String massOrganizationId = UUID.randomUUID().toString();
        User organizationUser = new User();
        organizationUser.setUsername(organization.getPhone());
        organizationUser.setPassword("123456");
        organizationUser.setTypes(3);
        organizationUser.setMassOrganizationId(massOrganizationId);
        User usered = iUserService.reg(organizationUser);
        //创建社联的基础信息
        User user = userMapper.getById(id);
        Date now = new Date();
        organization.setCreatedUser(user.getUsername());
        organization.setModifiedUser(user.getUsername());
        organization.setCreatedTime(now);
        organization.setModifiedTime(now);
        organization.settUserId(usered.getTUserId());
        organization.setMassOrganizationId(massOrganizationId);
        organization.setIdentity(2);
        organization.setApplicationStatus(1);
        Integer row = addOrganizationByCouncil(organization);
        return row;
    }

    @Override
    public Map<String, Object> getOrganizationsList(OrganizationVO organizationVO) {
        Map<String, Object> res = new HashMap<>();
        organizationVO.setIdentity(2);
        organizationVO.setCurrentPage(getCurrentPage(organizationVO.getCurrentPage(),organizationVO.getPageSize()));
        List<Organization> data = getOrganizationList(organizationVO);
        Integer totle = getOrganizationListTotal(organizationVO);
        res.put("data", data);
        res.put("totle", totle);
        return res;
    }

    @Override
    public List<Organization> getAPPOrganizations(OrganizationVO organizationVO) {
        if(organizationVO.getOrganization() !=null && organizationVO.getOrganization()!= "") {
            organizationVO.setIdentity(2);
            return getAPPOrganizationList(organizationVO);
        }else {
            return null;
        }

    }

    @Override
    @Transactional
    public Integer createAssociation(Organization organization, Integer id) {
        String massOrganizationId = UUID.randomUUID().toString();
        Date now = new Date();
        User user = userMapper.getById(id);
        //创建user信息
        User updateUser = new User();
        updateUser.setId(id);
        updateUser.setMassOrganizationId(massOrganizationId);
        updateUser.setTypes(2);
        updateUser.setModifiedUser(user.getUsername());
        updateUser.setModifiedTime(now);
        updateInfo(updateUser);
        //创建社团信息
        organization.setMassOrganizationId(massOrganizationId);
        organization.setIdentity(1);
        organization.settUserId(user.getTUserId());
        organization.setApplicationStatus(0);
        organization.setCreatedUser(user.getUsername());
        organization.setCreatedTime(now);
        organization.setModifiedUser(user.getUsername());
        organization.setModifiedTime(now);
        addOrganizationByCouncil(organization);
        return null;
    }

    @Override
    public Map<String, Object> getCommunitytionsList(OrganizationVO organizationVO, Integer id) {
        User user = userMapper.getById(id);
        Map<String, Object> res = new HashMap<>();
        organizationVO.setCurrentPage(getCurrentPage(organizationVO.getCurrentPage(),organizationVO.getPageSize()));
        organizationVO.setIdentity(1);
        organizationVO.setSuperiorsId(user.getMassOrganizationId());
        List<Organization> data = getOrganizationList(organizationVO);
        Integer totle = getOrganizationListTotal(organizationVO);
        res.put("data", data);
        res.put("totle", totle);
        return res;
    }

    @Override
    public Integer updateApply(Organization organization, Integer id) {
        User user = userMapper.getById(id);
        Date now = new Date();
        organization.setModifiedUser(user.getUsername());
        organization.setModifiedTime(now);
        organization.setApplicationStatus(1);
        updateApplicationStatus(organization);
        return null;
    }

    @Override
    public List<Organization> getAPPCommunitytionss(OrganizationVO organizationVO) {
        if(organizationVO.getOrganization() !=null && organizationVO.getOrganization()!= "") {
            organizationVO.setIdentity(1);
            return getAPPOrganizationList(organizationVO);
        }else {
            return null;
        }

    }

    @Override
    public List<Organization> getSingleCommunitytionss() {
        OrganizationVO organizationVO = new OrganizationVO();
        organizationVO.setIdentity(1);
        return getAPPOrganizationList(organizationVO);
    }

    /**
     * 常见社联
     * @param organization
     * @return
     */
    private Integer addOrganizationByCouncil(Organization organization){
        return organizationMapper.addOrganizationByCouncil(organization);
    };
    /**
     * 获得社联列表
     * @param organizationVO
     * @return
     */
    private List<Organization> getOrganizationList(OrganizationVO organizationVO) {
        return organizationMapper.getOrganizationList(organizationVO);
    };

    /**
     * 获得社联列表的总条数
     * @param organizationVO
     * @return
     */
    private Integer getOrganizationListTotal(OrganizationVO organizationVO){
        return organizationMapper.getOrganizationListTotal(organizationVO);
    };

    private Integer getCurrentPage(Integer currentPage, Integer pageSize){
        return (currentPage - 1) * pageSize;
    }
    /**
     * 获取社联列表
     * @param organizationVO
     * @return
     */
    private List<Organization> getAPPOrganizationList(OrganizationVO organizationVO){
        return organizationMapper.getAPPOrganizationList(organizationVO);
    };
    /**
     * 修改个人信息
     * @param user  用户数据
     * @return  受影响的行数
     */
    private Integer updateInfo(User user){
        return userMapper.updateInfo(user);
    };
    /**
     * 审核通过社团申请
     * @param organization
     * @return
     */
    private Integer updateApplicationStatus(Organization organization){
        return organizationMapper.updateApplicationStatus(organization);
    };
}
